﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web.UI;

public partial class Main_Admin_CreateCompany : System.Web.UI.Page
{
    Datautility du = new Datautility();
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void Savexml()
    {
        DataSet ds = new DataSet();
        try
        {
           ds.ReadXml(Server.MapPath("~/App_Data/ConnStr.xml"));
           DataTable dt = ds.Tables[0];
            if(ds.Tables.Count > 0)
               {
                   bool val= true;
                   for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                   {
                       if (dt.Rows[i]["companyname"].ToString() == TxtCOmpanyName.Text)
                       {
                           val = false;
                           break;

                       }
                   }
                   if( val == true)
                    {
                           DataRow dr = ds.Tables[0].NewRow();
                           // LblMsg.Text = ds.Tables[0].ToString();
                           dr["companyname"] = TxtCOmpanyName.Text.Trim();
                           dr["dbname"] = TxtDatabasename.Text.Trim();
                           dr["servername"] = TxtServerName.Text.Trim();
                           dr["userid"] = TxtUserId.Text.Trim();
                           dr["password"] = TxtPassword.Text.Trim();
                          // LblMsg.Text += dt.Rows[i]["companyname"].ToString();   
                        
                            ds.Tables[0].Rows.Add(dr);
                            ds.WriteXml(Server.MapPath("~/App_Data/ConnStr.xml"));
                            Session["constr123"] = "Data Source=" + TxtServerName.Text + ";Initial Catalog=" + TxtDatabasename.Text + ";user id=" + TxtUserId.Text + ";password=" + TxtPassword.Text + ";Persist Security Info=False";
                            ExecuteScript(Server.MapPath("~/SqlScript/TableScript.sql"));
                            ExecuteScript(Server.MapPath("~/SqlScript/ViewScript.sql"));
                            ExecuteScript(Server.MapPath("~/SqlScript/FunctionScript.sql"));
                            ExecuteScript(Server.MapPath("~/SqlScript/ProcedureScript.sql"));
                            TxtDatabasename.Text = "";
                            TxtServerName.Text = "";
                            TxtUserId.Text = "";
                            TxtPassword.Text = "";
                            Response.Redirect("NewCompany.aspx?compname=" + TxtCOmpanyName.Text);
                            //LblMsg.Text = "saved successfully";
                           
                    }
                   else
                    {
                        LblMsg.Text = "Another Company Exist with same name";
                       
                    }
                }
        }
        catch (Exception ex)
        {
            LblMsg.Text = ex.Message;
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Savexml();
        //Response.Redirect("NewCompany.aspx?compname="+ TxtCOmpanyName.Text +"&databasename="+ TxtDatabasename.Text +"&ServerNAme="+ TxtServerName +"&userid"+ TxtUserId.Text +"password");
    }

    protected void ExecuteScript(string scriptfilepath)
    {
        try
        {
            StreamReader sr = new StreamReader(scriptfilepath);
            SqlConnection con = new SqlConnection(Session["constr123"].ToString());
            con.Open();
            while (!sr.EndOfStream)
            {
                StringBuilder SB = new StringBuilder();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandTimeout = 0;
                while (!sr.EndOfStream)
                {
                    string str = sr.ReadLine();
                    if (str != null && str.ToUpper().Trim().Equals("GO"))
                    {
                        break;
                    }
                    SB.AppendLine(str);
                }
                cmd.CommandText = SB.ToString();
                cmd.ExecuteNonQuery();
            }
        }
        catch (Exception ex)
        {
            LblMsg.Text = ex.Message;
        }
                              
     }
    protected void btncancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewCompany.aspx");
    }
}
